package scottb.flex;

import java.awt.*;

public class HFormItem extends HGroup {

    private String label;
    private String hint;
    private boolean required;
    private String error;

    private int labelWidth = 200;

    private FlexLabel labelField = new FlexLabel().width(125);
    private HintLabel hintField = new HintLabel();
    private ErrorLabel errorField = new ErrorLabel();

    private VGroup vgroup = new VGroup(hintField);

    private Component item;

    public HFormItem() {

        add(labelField);
        add(vgroup);
        add(errorField);
        gap(5);
        align(VAlign.CENTER);
    }

    public HFormItem(String label) {
        this();
        this.label = label;
        labelField.setText(label);
    }

    public HFormItem(String label, Component item) {
        this(label);
        this.item = item;
        vgroup.add(item, 0);
    }

    // ITEM
    public HFormItem item(Component item) {

        if(null != item) {
            vgroup.remove(item);
        }

        vgroup.add(item, 0);

        Component oldItem = this.item;
        this.item = item;

        firePropertyChange("item", oldItem, item);

        return this;
    }

    public Component item() {
        return item;
    }

    public void setItem(Component item) {
        item(item);
    }

    public Component getItem() {
        return item;
    }

    // LABEL
    public HFormItem label(String label) {

        String oldLabel = this.label;
        this.label = label;

        labelField.setText(label);

        firePropertyChange("label", oldLabel, label);

        return this;
    }

    public String label() {
        return label;
    }

    public void setLabel(String label) {
        label(label);
    }

    public String getLabel() {
        return label;
    }

    // HINT
    public HFormItem hint(String hint) {

        String oldHint = this.hint;
        this.hint = hint;

        hintField.setText(hint);

        firePropertyChange("hint", oldHint, hint);

        return this;
    }

    public String hint() {
        return hint;
    }

    public void setHint(String hint) {
        hint(hint);
    }

    public String getHint() {
        return hint;
    }

    // ERROR
    public HFormItem error(String error) {

        String oldError = this.error;
        this.error = error;

        errorField.setText(error);

        firePropertyChange("error", oldError, error);

        return this;
    }

    public String error() {
        return error;
    }

    public void setError(String error) {
        error(error);
    }

    public String getError() {
        return error;
    }

    // REQUIRED
    public HFormItem required(boolean required) {

        boolean oldRequired = this.required;
        this.required = required;

        firePropertyChange("required", oldRequired, required);

        return this;
    }

    public boolean required() {
        return required;
    }

    public void setRequired(boolean required) {
        required(required);
    }

    public boolean getRequired() {
        return required;
    }

    // LABEL WIDTH
    public HFormItem labelWidth(int labelWidth) {

        int oldLabelWidth = this.labelWidth;
        this.labelWidth = labelWidth;

        firePropertyChange("labelWidth", oldLabelWidth, labelWidth);

        return this;
    }

    public int labelWidth() {
        return labelWidth;
    }

    public void setLabelWidth(int labelWidth) {
        labelWidth(labelWidth);
    }

    public int getLabelWidth() {
        return labelWidth;
    }
}
